package com.moon.back.service.impl.handler;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord;
import com.hankcs.hanlp.dependency.IDependencyParser;
import com.hankcs.hanlp.dependency.nnparser.NeuralNetworkDependencyParser;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;

import java.util.List;

public class HanLPJavaDemo {
    static {
    HanLP.Config.NNParserModelPath  = "D:\\bishe\\moon_back\\moon_db\\data\\model\\dependency\\NNParserModel.txt";
        HanLP.Config.PerceptronCWSModelPath = "D:\\bishe\\moon_back\\moon_db\\data\\model\\perceptron\\large\\cws.bin";
        HanLP.Config.PerceptronPOSModelPath = "D:\\bishe\\moon_back\\moon_db\\data\\model\\perceptron\\pku1998\\pos.bin";
        HanLP.Config.PerceptronNERModelPath = "D:\\bishe\\moon_back\\moon_db\\data\\model\\perceptron\\pku1998\\ner.bin";

    }
    // 依存句法分析器
    private static final IDependencyParser dependencyParser = new NeuralNetworkDependencyParser();

    public static void main(String[] args) {
        String text = "删除名字包含test的文件夹";
        
        // 1. 分词 + 词性标注
        List<Term> termList = StandardTokenizer.segment(text);
        
        // 2. 依存句法分析
        CoNLLSentence sentence = dependencyParser.parse(termList);
        
        // 3. 格式化输出结果
        System.out.println(formatResult(termList, sentence));
    }

    // 组合词性标注和依存分析的结果
    private static String formatResult(List<Term> terms, CoNLLSentence sentence) {
        StringBuilder sb = new StringBuilder();
        
        // 词性标注输出
        sb.append("词性标注:\n");
        for (Term term : terms) {
            sb.append(String.format("%s/%s  ", term.word, term.nature));
        }
        sb.append("\n\n");
        
        // 依存句法分析输出
        sb.append("依存句法分析:\n");
        for (CoNLLWord word : sentence.getWordArray()) {
            CoNLLWord head = word.HEAD;
            String relation = word.DEPREL;
            String output = String.format("%s(%s) --> %s(%s) [%s]",
                    word.LEMMA, word.POSTAG,
                    (head != null) ? head.LEMMA : "ROOT",
                    (head != null) ? head.POSTAG : "ROOT",
                    relation);
            sb.append(output).append("\n");
        }
        
        return sb.toString();
    }
}